//
//  MainViewController.swift
//  Weibo2
//
//  Created by 伏文东 on 2018/2/6.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    /// 撰写按钮
    private lazy var composeButton: UIButton = UIButton.imageButton(
        imageName: "tabbar_compose_icon_add",
        backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildControllers()
        setupComposeButton()
        
        delegate = self
        
    }
    
    // MARK: - 监听方法
    @objc private func composeStatus()  {
        print("撰写 ")
    }
    
}

extension MainViewController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
        return !viewController.isMember(of: UIViewController.self)
    }
}

private extension MainViewController {
    
    /// 添加按钮
    func setupComposeButton()  {
        tabBar.addSubview(composeButton)
        let count = CGFloat(childViewControllers.count)
        
        let w = tabBar.bounds.width / count
        
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    /// 添加自控制器
    func setupChildControllers()  {
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
        
        var data = NSData(contentsOfFile: jsonPath)
        
        if data == nil {
            let path = Bundle.main.path(forResource: "main.json", ofType: nil)
            data = NSData(contentsOfFile: path!)
        }
        guard let array = try? JSONSerialization.jsonObject(with: data! as Data, options: []) as? [[String: Any]] else {
            return
        }
        
        var arrayM = [UIViewController]()
        
        for dict in array! {
            arrayM.append(controller(dict: dict))
        }
        viewControllers = arrayM
        
    }
    
    func controller(dict: [String: Any]) -> UIViewController {
        guard let clsName = dict["clsName"] as? String,
            let title = dict["title"] as? String,
            let imageName = dict["imageName"] as? String,
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? BaseViewController.Type,
            let VisitorDict = dict["visitorInfo"] as? [String: String]
            else { return UIViewController() }
        let vc = cls.init()
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.orange], for: .highlighted)
        let nav = NaviViewController(rootViewController: vc)
        return nav
    }
    
}
































